
package com.compuvest.admin.request;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
// </editor-fold>

public class MultiPart implements Serializable {

    private Map<String,String> parameters;
    private Map<String,FileItem> files;

// <editor-fold defaultstate="collapsed" desc="MultiPart()">

    public MultiPart() {
        this(null, 0);
    }

    public MultiPart(HttpServletRequest request, int maxSize) {

        if(ServletFileUpload.isMultipartContent(request)) {

            parameters = new HashMap<String,String>();
            files = new HashMap<String,FileItem>();

            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            diskFileItemFactory.setSizeThreshold(maxSize); // the unit is bytes in memory

            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
            servletFileUpload.setSizeMax(maxSize); // the unit is bytes

            try {
                List fileItemsList = servletFileUpload.parseRequest(request);
//System.out.println("########["+fileItemsList.toString()+"]");
                Iterator it = fileItemsList.iterator();
                while(it.hasNext()) {
                    FileItem fileItem = (FileItem)it.next();
                    if(fileItem.isFormField()) {
                        if(fileItem.getSize() != 0) {
                            parameters.put(fileItem.getFieldName(), fileItem.getString());
                        }
                    }
                    else {
                        files.put(fileItem.getFieldName(), fileItem);
                    }
                }
            }
            catch(FileUploadException ex) {
                System.out.println("admin::MultiPart:error: " + ex.toString() + "\nSKU=" + request.getParameter("accwareCatId") + "\naddress=" + request.getRemoteAddr());
            }
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getParameter()">

    public String getParameter(String name) {
        return parameters.get(name);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getParameter()">

    public String getParameter(String name, String defValue) {
        return parameters.get(name) == null ? defValue : parameters.get(name);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getStringNoQuote()">

    public String getStringNoQuote(String name) {
        return parameters.get(name) == null ? "" : parameters.get(name).replace("\'", "&#39;").replace("\"", "&#34;").trim();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getStringDelQuote(String name)">

    public String getStringDelQuote(String name) {
        return parameters.get(name) == null ? "" : parameters.get(name).replace("\'", "").replace("\"", "").trim();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getActive(String name)">

    public boolean getActive(String name) {
        return parameters.get(name) == null ? false : "Y".equalsIgnoreCase(parameters.get(name)) ? true : false;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getInt(String name)">

    public int getInt(String name) {
        int value = 0;
        try { 
            value = parameters.get(name) == null ? 0 : Integer.parseInt(parameters.get(name));
        } catch(Exception ignore) { }

        return value;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getLong(String name)">

    public long getLong(String name) {
	long value = 0L;
	try {
            value = parameters.get(name) == null ? 0L : Long.parseLong(parameters.get(name));
	}
	catch(Exception ignore) { }

	return value;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getDouble(String name)">

    public double getDouble(String name) {
	double value = 0d;
	try { 
            value = parameters.get(name) == null ? 0d : Double.parseDouble(parameters.get(name));
        } catch(Exception ignore) { }

	return value;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getDescription()">

    public String getDescription() {
        return files.get("description") == null ? null : files.get("description").getString();//.replace("\'", "&#39;");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="imageSize(String name)">

    public long imageSize(String name) {
        return files.get(name) == null ? 0 : files.get(name).getSize();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getImage(String name)">

    public byte[] getImageFromMemory(String name) throws IOException {
        return files.get(name) == null ? null : files.get(name).get();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="saveExcelFile()">

    public int saveExcelFile(String name, String path, String fileName) {

        FileItem fileItem = files.get(name);
        if(fileItem != null) {
            File file = new File(path + fileName);
            if(fileItem.getSize() > 0) {
                try {
                    fileItem.write(file);

                    return (int)fileItem.getSize();
                }
                catch(Exception ignore) { }
            }
            else { // if size = 0 delete file
                if(file.exists()) {
                    file.delete();
                }
            }
        }
        return 0;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toString()">

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
//        sb.append("=[").append(parameters.get("")).append("]\n");
        sb.append("===========").append(parameters.get("accwareItemCode")).append("============\n");
        sb.append("active=[").append(parameters.get("active")).append("]\n");
        sb.append("stockQuantity=[").append(parameters.get("stockQuantity")).append("]\n");
        sb.append("stock=[").append(parameters.get("stock")).append("]\n");
        sb.append("bannerMask=[").append(parameters.get("bannerMask")).append("]\n");
        sb.append("advertMask=[").append(parameters.get("advertMask")).append("]\n");
        sb.append("upccode=[").append(parameters.get("upccode")).append("]\n");
        sb.append("vendor=[").append(parameters.get("vendor")).append("]\n");
        sb.append("accwareCatId=[").append(parameters.get("accwareCatId")).append("]\n");
        sb.append("itemName=[").append(parameters.get("itemName")).append("]\n");
        sb.append("mfrName=[").append(parameters.get("mfrName")).append("]\n");
        sb.append("mfrNumber=[").append(parameters.get("mfrNumber")).append("]\n");
        sb.append("mfrLink=[").append(parameters.get("mfrLink")).append("]\n");
        sb.append("advPrice=[").append(parameters.get("advPrice")).append("]\n");
        sb.append("packageInclude=[").append(parameters.get("packageInclude")).append("]\n");
        sb.append("packageComments=[").append(parameters.get("packageComments")).append("]\n");
        sb.append("stockDate=[").append(parameters.get("stockDate")).append("]\n");
        sb.append("drone=[").append(parameters.get("drone")).append("]\n");
        sb.append("costPrice=[").append(parameters.get("costPrice")).append("]\n");
        sb.append("sellPrice=[").append(parameters.get("sellPrice")).append("]\n");
        sb.append("hotPrice=[").append(parameters.get("hotPrice")).append("]\n");
        sb.append("resPrice1=[").append(parameters.get("resPrice1")).append("]\n");
        sb.append("resPrice2=[").append(parameters.get("resPrice2")).append("]\n");
        sb.append("resPrice3=[").append(parameters.get("resPrice3")).append("]\n");
        sb.append("resQuantity2=[").append(parameters.get("resQuantity2")).append("]\n");
        sb.append("resQuantity3=[").append(parameters.get("resQuantity3")).append("]\n");
        sb.append("weight=[").append(parameters.get("weight")).append("]\n");
        sb.append("warrantyMask=[").append(parameters.get("warrantyMask")).append("]\n");
        sb.append("warrantyPrice1=[").append(parameters.get("warrantyPrice1")).append("]\n");
        sb.append("warrantyPrice2=[").append(parameters.get("warrantyPrice2")).append("]\n");
        sb.append("warrantyPrice3=[").append(parameters.get("warrantyPrice3")).append("]\n");
        sb.append("vendor=[").append(parameters.get("vendor")).append("]\n");
        sb.append("=================================\n");

        return sb.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="finalize()">

    @Override
    protected void finalize() throws Throwable {
        super.finalize();

        parameters = null;
        files = null;
    }
// </editor-fold>

}
